package com.junchenrobot.mes.dao;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.junchenrobot.mes.domain.InputTask;
import com.junchenrobot.mes.domain.WinderTask;
import com.junchenrobot.mes.domain.WinderTaskRun;
import com.junchenrobot.mes.mapper.WinderTaskRunMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 小车任务Dao
 *
 * @author 
 * @date 202/2/11
 */
@Slf4j
@Component
public class WinderTaskRunDao {
    
    private final WinderTaskRunMapper winderTaskRunMapper;
    
    @Autowired
    public WinderTaskRunDao(WinderTaskRunMapper winderTaskRunMapper) {
        this.winderTaskRunMapper = winderTaskRunMapper;
    }

    /**
     * 获取任务
     *
     * @param taskCode 任务代码
     * @return {@link InputTask}
     */
    public WinderTaskRun getTaskRun(String taskCode) {
        return new LambdaQueryChainWrapper<>(winderTaskRunMapper).eq(WinderTaskRun::getTaskCode, taskCode).last("LIMIT 1").one();
    }


    /**
     * 根据任务id查询任务
     *
     * @param taskId 任务id
     * @return {@link WinderTask}
     */
    public WinderTaskRun getWinderTask(Long taskId) {
        return new LambdaQueryChainWrapper<>(winderTaskRunMapper).eq(WinderTaskRun::getId, taskId).last(true, "LIMIT 1").one();
    }
    /**
     * 插入一条备选任务
     *
     * @param winderTaskRun 备选任务
     * @return int
     */
    public int insertNewTask(WinderTaskRun winderTaskRun) {
        return winderTaskRunMapper.insert(winderTaskRun);
    }

    /**
     * 查询所有小车的任务
     *
     * @return {@link List}<{@link WinderTaskRun}>
     */
    public List<WinderTaskRun> listUnFinishedTask() {
        return new LambdaQueryChainWrapper<>(winderTaskRunMapper)
                 .list();
    }

    /**
     * 删除任务
     *
     * @param winderTaskRun 任务
     */
    public void deleteWinderTask(WinderTaskRun winderTaskRun) {
         winderTaskRunMapper.deleteById(winderTaskRun);
    }

        public void deleteAll(){
    winderTaskRunMapper.deleteAll();
}

}
